//
//  Home_BirthCell.m
//  HuaRun2
//
//  Created by admin on 17/9/5.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "Home_BirthCell.h"
#import "Home_BirthCollectionCell.h"
#import "Home_BirthModel.h"

@interface Home_BirthCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong)UICollectionView * collectionView;

@end

static NSString * const indentifier = @"Home_BirthCell";

@implementation Home_BirthCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

#pragma mark - life cycle
- (void)dealloc {
    NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    self.contentView.backgroundColor = RGBA(249, 249, 249, 1);
    
    [self.contentView addSubview:self.collectionView];
}

- (void)layoutInit {
    //    weakself(ws);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    Home_BirthCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:indentifier
                                                                                 forIndexPath:indexPath];
    cell.birthModel = _dataArray[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSLog(@"%ld", indexPath.row);
    Home_BirthModel * model = _dataArray[indexPath.row];    
    self.clickBlock ? self.clickBlock(model.id) : nil;
}

#pragma mark - getter setter
- (void)setDataArray:(NSArray *)dataArray {
    _dataArray = dataArray;
    [self.collectionView reloadData];
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [UICollectionViewFlowLayout new];
        CGFloat itemWidth = (kMainScreenWidth - 20 - 5)/ 2;
        CGFloat itemHeight = 60;
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumLineSpacing = 5;
        layout.minimumInteritemSpacing = 5;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:(CGRectMake(10, 0, kMainScreenWidth - 20, 60))
                                                 collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[Home_BirthCollectionCell class]
            forCellWithReuseIdentifier:indentifier];
        _collectionView.backgroundColor =  RGBA(249, 249, 249, 1);
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.bounces= NO;
    }
    return _collectionView;
}

@end
